package com.egao.common.remark.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.article.entity.TArticle;
import com.egao.common.article.entity.dto.ArticleDto;
import com.egao.common.article.service.ITArticleService;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.remark.entity.TArticleRemark;
import com.egao.common.remark.entity.TCourseRemark;
import com.egao.common.remark.entity.dto.CourseRemarkDto;
import com.egao.common.remark.mapper.TCourseRemarkMapper;
import com.egao.common.remark.service.ITCourseRemarkService;
import com.egao.common.system.entity.UserTwo;
import com.egao.common.system.service.UserService;
import com.egao.common.system.service.UserTwoService;
import com.egao.common.test.service.CourseService;
import com.egao.common.test.service.MenurService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @auther ZhaoPeng Wang
 * @create 2022-03-21 21:51:10
 * @description 前端控制器
 */
@Controller
@RequestMapping("/course-remark")
@Slf4j
public class TCourseRemarkController {
    @Autowired
    MenurService menurService;

    @Autowired
    CourseService courseService;

    @Autowired
    UserService userService;

    @Autowired
    UserTwoService userTwoService;

    @Autowired
    ITCourseRemarkService itCourseRemarkService;

    @Autowired
    TCourseRemarkMapper tCourseRemarkMapper;

    @RequestMapping()
    public String view(Model model) {
        model.addAttribute("remarkList", itCourseRemarkService.list());
        return "remark/course-remark.html";
    }

    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/page")
    public PageResult<TCourseRemark> page(HttpServletRequest request) {
        PageParam<TCourseRemark> pageParam = new PageParam<>(request);
        return new PageResult<>(itCourseRemarkService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
        //return classifyService.listPage(pageParam);  // 使用关联查询
    }



    /**
     * 查询全部
     */
    @ResponseBody
    @RequestMapping("/list")
    public JsonResult list(HttpServletRequest request) {
        PageParam<TCourseRemark> pageParam = new PageParam<>(request);
        return JsonResult.ok().setData(itCourseRemarkService.list(pageParam.getOrderWrapper()));
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Integer id) {
        return JsonResult.ok().setData(itCourseRemarkService.getById(id));
    }


    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/getContent")
    public String getContent(Integer id) {
        String res = "";
        TCourseRemark byId = itCourseRemarkService.getById(id);
        if (byId != null) {
            res = byId.getContent();
        }
        return res;
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/getByContent")
    public PageResult<TCourseRemark> getByContent(String content) {
        QueryWrapper<TCourseRemark> wrapper = new QueryWrapper<>();
        wrapper.like("content",content);
        PageParam<TCourseRemark> pageParam = new PageParam<>();
        return new PageResult<>(itCourseRemarkService.page(pageParam, pageParam.getOrderWrapper(wrapper)).getRecords(), pageParam.getTotal());
    }



    /**
     * 审核
     * @param id
     * @param state
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/shenhe")
    public JsonResult ShenHe(Integer id,Integer state,Integer userId) {
        TCourseRemark remark = itCourseRemarkService.getById(id);
        UserTwo usertwo = userTwoService.getById(userId);
        remark.setState(state);
        if (itCourseRemarkService.updateById(remark)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(String id,String content) {
//        QueryWrapper<TArticleRemark> wrapper  = new QueryWrapper<>();
//        wrapper.eq("content",content);
//        tArticleRemarkMapper.selectOne(wrapper);
        log.info("看这里------->: {}",id);
        log.info("看这里------->: {}",content);


        log.info("看这里------->: {}",id.toString());
        StringBuffer sb = new StringBuffer();
        for (char c : id.toCharArray()){
            if (Character.isDigit(c)){
                sb.append(c);
            }
        }
        log.info("看这里------->: {}",sb.toString());
        int remarkId = Integer.parseInt(sb.toString());
        TCourseRemark parent = itCourseRemarkService.getById(id);

        TCourseRemark remark = new TCourseRemark();
        Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        Integer userId;
        if (object != null) {
            try {
                userId = ((UserTwo) object).getUserId();
            }catch (ClassCastException e){
                userId = 0;

            }
            remark.setContent(content);
            remark.setName(content);
            remark.setUserId(userId);
            remark.setCreateTime(new Date());
            remark.setParentId(remarkId);
            remark.setState(0);
            remark.setCourseId(parent.getCourseId());
            if (itCourseRemarkService.save(remark)) {
                return JsonResult.ok("添加成功");
            }else {
                return JsonResult.error("添加失败");
            }
        }else {
            return JsonResult.error("请先登录");
        }

    }

    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("/saveBatch")
    public JsonResult saveBatch(@RequestParam List<String> ids, String content) {
//        QueryWrapper<TArticleRemark> wrapper  = new QueryWrapper<>();
//        wrapper.eq("content",content);
//        tArticleRemarkMapper.selectOne(wrapper);

        log.info("看这里------->: {}",ids.toString());
        log.info("看这里------->: {}",content);
        String s = ids.get(0);
        log.info("看这里------->: {}",s.toString());
        StringBuffer sb = new StringBuffer();
        for (char c : s.toCharArray()){
            if (Character.isDigit(c)){
                sb.append(c);
            }
        }
        log.info("看这里------->: {}",sb.toString());
        int id = Integer.parseInt(sb.toString());
        TCourseRemark parent = itCourseRemarkService.getById(id);

        TCourseRemark remark = new TCourseRemark();
        Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        Integer userId;
        if (object != null) {
            try {
                userId = ((UserTwo) object).getUserId();
            }catch (ClassCastException e){
                userId = 0;

            }
            remark.setContent(content);
            remark.setName(content);
            remark.setUserId(userId);
            remark.setCreateTime(new Date());
            remark.setParentId(id);
            remark.setState(0);
            remark.setCourseId(parent.getCourseId());
            if (itCourseRemarkService.save(remark)) {
                return JsonResult.ok("添加成功");
            }else {
                return JsonResult.error("添加失败");
            }
        }else {
            return JsonResult.error("请先登录");
        }
    }


    /**
     * 删除文章
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult deleteArticle(Integer id) {
        if (itCourseRemarkService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(CourseRemarkDto dto) {
        TCourseRemark remark = new TCourseRemark();
        BeanUtils.copyProperties(dto,remark);
        if (itCourseRemarkService.updateById(remark)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("/removeBatch")
    public JsonResult removeBatch(@RequestBody List<Integer> ids) {
        if (itCourseRemarkService.removeByIds(ids)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }



}

